package com.example.lancooznbk.view.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.hmy.popwindow.PopWindow;

import java.util.List;

/**
 * Created by Administrator on 2018\1\19 0019.
 */

public class DisciplinePopWindow extends PopupWindow {

    private final PopWindow PopWindows;
    private View mMenuView;

    private TextView mTvDisChinese;
    private TextView mTvDisMathematics;
    private TextView mTvDisEnglish;
    private TextView mTvDisChemical;
    private TextView mTvDisPhysical;
    private TextView mTvDisBiological;
    private TextView mTvDisGeographic;
    private TextView mTvDisHistory;
    private TextView mTvDisPolitical;
    private Activity Windowcontext;
    private ImageView WindowmIv;
    private View WindowmView;
    private List<String> head_titles;
    private List<String> SubjectID;
    private Drawable top;
    private ClickListenerInterface clickListenerInterface;

    private TextView mTvDisNameA;

    public interface ClickListenerInterface {
        public void doChangeSubject(String subjectName);
    }

    public DisciplinePopWindow(final Activity context, TextView mTv, ImageView mIv, View mView, List<String> head_titles, List<String> SubjectID){
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.head_titles = head_titles;
        this.SubjectID = SubjectID;
        mMenuView = inflater.inflate(R.layout.layout_discipline_window, null);
        initView();
        PopWindows = new PopWindow.Builder(context)
                .setStyle(PopWindow.PopWindowStyle.PopDown)
                .setView(mMenuView)
                .setControlViewAnim(mIv, R.anim.btn_rotate_anim_1, R.anim.btn_rotate_anim_2, true)
                .show(mView);

        mTvDisNameA = mTv;
        Windowcontext =context;
        WindowmIv =mIv;
        WindowmView =mView;

    }

    private void initView(){
        mTvDisChinese = (TextView) mMenuView.findViewById(R.id.tv_chinese);
        mTvDisMathematics = (TextView) mMenuView.findViewById(R.id.tv_mathematics);
        mTvDisEnglish = (TextView) mMenuView.findViewById(R.id.tv_english);
        mTvDisChemical = (TextView) mMenuView.findViewById(R.id.tv_chemical);
        mTvDisPhysical = (TextView) mMenuView.findViewById(R.id.tv_physical);
        mTvDisBiological = (TextView) mMenuView.findViewById(R.id.tv_biological);
        mTvDisGeographic = (TextView) mMenuView.findViewById(R.id.tv_geographic);
        mTvDisHistory = (TextView) mMenuView.findViewById(R.id.tv_history);
        mTvDisPolitical = (TextView) mMenuView.findViewById(R.id.tv_political);

        clickChinese();
        clickMathematics();
        clickEnglish();
        clickChemical();
        clickPhysical();
        clickBiological();
        clickGeographic();
        clickHistory();
        clickPolitical();
        setData();
    }

    private void  setData(){
        for (int i=0;i< head_titles.size();i++ ){
            Log.e("setData","head_titles.get(i)+"+head_titles.get(i));
            if (i==0){
                setContent(mTvDisChinese,head_titles.get(i),SubjectID.get(i));
                mTvDisChinese.setVisibility(View.VISIBLE);
            }else if (i==1){
                setContent(mTvDisMathematics,head_titles.get(i),SubjectID.get(i));
                mTvDisMathematics.setVisibility(View.VISIBLE);
            }else if (i==2){
                setContent(mTvDisEnglish,head_titles.get(i),SubjectID.get(i));
                mTvDisEnglish.setVisibility(View.VISIBLE);
            }else if (i==3){
                setContent(mTvDisChemical,head_titles.get(i),SubjectID.get(i));
                mTvDisChemical.setVisibility(View.VISIBLE);
            }else if (i==4){
                setContent(mTvDisPhysical,head_titles.get(i),SubjectID.get(i));
                mTvDisPhysical.setVisibility(View.VISIBLE);
            }else if (i==5){
                setContent(mTvDisBiological,head_titles.get(i),SubjectID.get(i));
                mTvDisBiological.setVisibility(View.VISIBLE);
            }else if (i==6){
                setContent(mTvDisGeographic,head_titles.get(i),SubjectID.get(i));
                mTvDisGeographic.setVisibility(View.VISIBLE);
            }else if (i==7){
                setContent(mTvDisHistory,head_titles.get(i),SubjectID.get(i));
                mTvDisHistory.setVisibility(View.VISIBLE);
            }else if (i==8){
                setContent(mTvDisPolitical,head_titles.get(i),SubjectID.get(i));
                mTvDisPolitical.setVisibility(View.VISIBLE);
            }
        }
    }

    private void  setContent( TextView ContentTV, String dataName, String subjectID){
        top=null;
        Resources res = ContentTV.getResources();
        ContentTV.setText(dataName);
        if (dataName.equals("语文")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_chinese);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("数学")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_mathematics);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("英语")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_english);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("化学")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_chemical);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("物理")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_physical);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("生物")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_biological);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("地理")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_geographic);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("历史")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_history);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }else if (dataName.equals("政治")){
            top = res.getDrawable(R.drawable.selector_btn_discipline_political);
            ContentTV.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
        }

    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }


    /*
    * 语文
    * */
    private void clickChinese(){
        mTvDisChinese.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisChinese.getText());
                clickListenerInterface.doChangeSubject("A");
                closeView();
            }
        });

    }

    /*
    * 数学
    * */
    private void clickMathematics(){
        mTvDisMathematics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisMathematics.getText());
                clickListenerInterface.doChangeSubject("B");
                closeView();
            }
        });

    }

    /*
    * 英语
    * */
    private void clickEnglish(){
        mTvDisEnglish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisEnglish.getText());
                clickListenerInterface.doChangeSubject("C");
                closeView();
            }
        });

    }

    /*
    * 物理
    * */
    private void clickBiological(){
        mTvDisBiological.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisBiological.getText());
                clickListenerInterface.doChangeSubject("D");
                closeView();
            }
        });

    }

    /*
    * 化学
    * */
    private void clickChemical(){
        mTvDisChemical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisChemical.getText());
                clickListenerInterface.doChangeSubject("E");
                closeView();
            }
        });

    }



    /*
    * 生物
    * */
    private void clickPhysical(){
        mTvDisPhysical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisPhysical.getText());
                clickListenerInterface.doChangeSubject("F");
                closeView();
            }
        });

    }

    /*
   * 政治
   * */
    private void clickPolitical(){
        mTvDisPolitical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisPolitical.getText());
                clickListenerInterface.doChangeSubject("G");
                closeView();
            }
        });

    }

    /*
    * 历史
    * */
    private void clickHistory(){
        mTvDisHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisHistory.getText());
                clickListenerInterface.doChangeSubject("H");
                closeView();
            }
        });

    }

    /*
    * 地理
    * */
    private void clickGeographic(){
        mTvDisGeographic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTvDisNameA.setText(mTvDisGeographic.getText());
                clickListenerInterface.doChangeSubject("I");
                closeView();
            }
        });

    }



    private void closeView() {
        //mVmasker.setVisibility(View.GONE);
        ;
        PopWindows.dismiss();
    }

}
